from setuptools import find_packages, setup, Command
from setuptools import find_namespace_packages
from os import walk
import glob
from os.path import join, dirname, sep
# from setuptools.command.build import build
from setuptools.command.build_py import build_py
import subprocess
import os
import shutil

class BuildPreprocessCommand(Command):
    """自定义编译前预处理命令"""
    description = '运行编译前脚本'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        """运行预处理脚本"""
        print("运行编译前脚本...")

def generateResource(_projectName):
    # print(['pyside6-rcc', os.path.join('.', _projectName, 'imports', 'resource.qrc'), "-o", os.path.join('.', _projectName, 'imports', 'resource_rc.py')])
    subprocess.run(['pyside6-rcc', os.path.join('.', _projectName, 'imports', 'resource.qrc'), "-o", os.path.join('.', _projectName, 'imports', 'resource_rc.py')])

def generateTranslations(projectName: str, localeDatas, files=None):
    if not files:
        files = []
    targetFolder = f"{os.path.join('.', projectName, 'imports', projectName, 'i18n')}"
    for locale in localeDatas:
        tsFileName = f"{projectName}_{locale}.ts"
        qmFileName = f"{projectName}_{locale}.qm"
        tsFilePath = f"{os.path.join('.', projectName, tsFileName)}"
        qmFilePath = f"{os.path.join('.', projectName, qmFileName)}"
        commands = ['pyside6-lupdate', os.path.join('.', projectName, 'imports', "resource.qrc")]
        for file in files:
            commands.append(file)
        commands.append("-ts")
        commands.append(tsFilePath)
        subprocess.run(commands)
        # print("OSmile-->1", commands)
        subprocess.run(['pyside6-lrelease', tsFilePath])
        # print("OSmile-->2", 'pyside6-lrelease', tsFilePath)
        os.makedirs(targetFolder, exist_ok=True)
        # print("OSmile-->3", qmFilePath, os.path.join(targetFolder, qmFileName))
        shutil.copy(qmFilePath, os.path.join(targetFolder, qmFileName))

class PreBuildBuild(build_py):
    def run(self):
        print("=== 运行编译前预处理 ===")

        # subprocess.run(['pyside6-lupdate', '.\\FluentUI\\imports\\resource.qrc', '-ts', '.\\FluentUI\\FluentUI_en_US.ts'], check=True)
        # subprocess.run(['pyside6-lrelease', '.\FluentUI\FluentUI_en_US.ts'])
        # shutil.copy()
        # subprocess.run(['pyside6-lupdate', '.\\FluentUI\\imports\\resource.qrc', '-ts', '.\\FluentUI\\FluentUI_zh_CN.ts'], check=True)
        # subprocess.run(['pyside6-lrelease', '.\FluentUI\FluentUI_zh_CN.ts'])
        # subprocess.run(['pyside6-lupdate', '.\\example\\imports\\resource.qrc', '.\\example\\helper\\InitializrHelper.py', '-ts', '.\\example\\example_en_US.ts'], check=True)
        # subprocess.run(['pyside6-lrelease', '.\example\example_en_US.ts'])
        # subprocess.run(['pyside6-lupdate', '.\\example\\imports\\resource.qrc', '.\\example\\helper\\InitializrHelper.py', '-ts', '.\\example\\example_zh_CN.ts'], check=True)
        # subprocess.run(['pyside6-lrelease', '.\example\example_zh_CN.ts'])

        generateTranslations("FluentUI", ["en_US", "zh_CN"])
        generateResource("FluentUI")

        print("=== 预处理完成，开始编译 ===")
        super().run()

setup(
    name='PySide6-FluentUI-QML',
    version='1.6.7',
    author="ROrange",
    description=('将PySide6-FluentUI-QML打包成whl安装包，便于pip安装'),
    author_email='whghcyx@outlook.com',
    url='https://github.com/zhuzichu520/PySide6-FluentUI-QML',
    packages=find_packages(exclude=('example', 'doc',)),
    # packages=find_packages(include=['FluentUI'], exclude=('example')),
    # packages=find_namespace_packages(include=['FluentUI'], exclude=('example')),
    package_data={
        '': ['**/*.qml',
            '**/*.js',
            '**/*.ttf',
            '*.ts',
            '*.qm'],
        'FluentUI':[
            'imports/*',
            'imports/FluentUI/*',
            'imports/FluentUI/Controls/*',
            'imports/FluentUI/Font/*',
            'imports/FluentUI/Image/*',
            'imports/FluentUI/i18n/*',
            'imports/FluentUI/JS/*',
    ]},
    include_package_data=True,
    install_requires=["pyside6==6.8.3", "qrcode",'keyboard','pillow'],
    setup_requires=["pyside6==6.8.3",'wheel'],
    cmdclass={
        'build_qt': BuildPreprocessCommand,
        'build_py': PreBuildBuild,
    },
)
